﻿using System;
using System.Collections.Generic;
using System.Text;
using MyIni;

namespace XhPaper.功能实现
{
    public class Dyhx:MyAutoCadObjectBase
    {
        /// <summary>
        /// 智能传感器列间的距离
        /// </summary>
        int cgqHangjianJu;
        /// <summary>
        /// 接口板列间距
        /// </summary>
        int jkbHangJIanJu;
        /// <summary>
        /// 传感器和接口板之间行间距
        /// </summary>
        int cgq_jkbHangjianju;
        /// <summary>
        /// 智能传感器接口板组和智能传感器接口板之间的距离
        /// </summary>
        int zJIanju;
        IniFile m_myini;// = new IniFile(Environment.CurrentDirectory+"\\dyhx.ini");
        /// <summary>
        /// 电源模块数量
        /// </summary>
        int m_dymodle_count;
        /// <summary>
        /// 电源模块列表
        /// </summary>
        List<MyTableDy> dyList = new List<MyTableDy>();
        /// <summary>
        /// 每个电源模块包含的智能传感器
        /// </summary>
        Dictionary<int, List<MyTableJdqMini>> cgqDic = new Dictionary<int, List<MyTableJdqMini>>();
        /// <summary>
        /// 每个电源模块包含的接口板数量
        /// </summary>
        Dictionary<int, List<MyTableJkbMini>> jkbDic = new Dictionary<int, List<MyTableJkbMini>>();
        /// <summary>
        /// 不同设备间的连线集合
        /// </summary>
        List<MyLine> m_lianXian = new List<MyLine>();
        List<string> m_cgqNames = new List<string>();
        List<string> m_jkbNames = new List<string>();
        MyTuZhi tuzhi ;
        public Dyhx(string _inifile="")
        {
            if (_inifile == "")
            {
                m_myini = new IniFile(Environment.CurrentDirectory + "\\dyhx.ini");
            }
            else
            {
                m_myini =new IniFile( _inifile);
            }
            tuzhi = new MyTuZhi(0, 0, null);
            SetCfg();
            SetLianXian();
        }
        public Dyhx(double left, double top,string _inifile="")
        {
            if (_inifile == "")
            {
                m_myini = new IniFile(Environment.CurrentDirectory + "\\dyhx.ini");
            }
            else
            {
                m_myini = new IniFile(_inifile);
            }
            tuzhi = new MyTuZhi(left, top, null);
            SetCfg();
            SetLianXian();
        }
        
        /// <summary>
        /// 设置各种电源连线
        /// </summary>
        private void  SetLianXian()
        {
            MyPoint p1,p2;
            MyLine line;
            for (int i = 3; i < 5; i++)
            {
                p1 = dyList[0].Dz1[i].P1;
                p2 = dyList[m_dymodle_count - 1].Dz1[i].P1.inflate(0, -40);
                line = new MyLine(p1, p2, this);
                m_lianXian.Add(line);
            }
            for (int i = 0; i < m_dymodle_count; i++)
            {
                #region 绘制传感器电源线
                int n = cgqDic[i].Count-1;
                p1 = dyList[i].Dz1[0].P1; 
                p2 = cgqDic[i][n].Dz1.P1;
                m_lianXian.Add(new MyLine(p1, p2, this));
                p1 = dyList[i].Dz1[1].P1;
                p2 = cgqDic[i][n].Dz2.P1;
                m_lianXian.Add(new MyLine(p1, p2, this));
                #endregion
                #region 接口板电源
                if ((n=jkbDic[i].Count) > 0)
                {
                    for (int i1 = 0; i1 < 3; i1++)
                    {
                        m_lianXian.Add(new MyLine(dyList[i].Dz1[i1].P1, p1 = dyList[i].Dz1[i1].P1.inflate(0, -5), this));
                        m_lianXian.Add(new MyLine(p1, p1 = p1.inflate(20-i1*3, 0), this));
                        m_lianXian.Add(new MyLine(p1, p1 = p1.inflate(0, -34), this));
                        m_lianXian.Add(new MyLine(p1,jkbDic[i][n-1].Dz[i1].P1));
                    }
                }
                #endregion
            }
        }
        /// <summary>
        /// 得到智能传感器的编号
        /// </summary>
        /// <param name="strSec"></param>
        private List<string> GetCgqNos(string strSec)
        {
            List<string> list=new List<string>();
            string str = m_myini.GetString(strSec, "智能传感器编号", "");
            string[] strs1 = str.Split(new char[]{';'},StringSplitOptions.RemoveEmptyEntries);
            foreach (string str2 in strs1)
            {
                string[] strs2 = str2.Split(new char[] {',' }, StringSplitOptions.RemoveEmptyEntries);
                int n, n0;
                int.TryParse(strs2[0].Substring(strs2[0].IndexOf('-')+1), out n0);
                if (strs2[1].Contains("-"))
                {
                    string str5 = strs2[1].Substring(strs2[1].IndexOf('-'), strs2[1].Length - strs2[1].IndexOf('-') - 1);
                    int.TryParse(str5, out n);
                    n = n - n0 + 1;
                }
                else
                {
                    int.TryParse(strs2[1], out n);
                }
                string str4=strs2[0].Substring(0,strs2[0].IndexOf('-'));
                for(int i=0;i<n;i++)
                {
                    string str3=str4+"-"+n0++;
                    list.Add(str3);
                }
            }
            return list;
        }
        private Dictionary<string,string> GetCfgNo_Style(string strSec)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string str = m_myini.GetString(strSec, "智能传感器编号", "");
            string style;
            string[] strs1 = str.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string str2 in strs1)
            {
                string[] strs2 = str2.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                int n, n0;
                int.TryParse(strs2[0].Substring(strs2[0].IndexOf('-') + 1), out n0);
                if (strs2[1].Contains("-"))
                {
                    int n5=strs2[1].IndexOf("-");
                    string str5 = strs2[1].Substring(n5+1, strs2[1].Length-n5-1);
                    int.TryParse(str5, out n5);
                    n = n5 - n0+1;
                }
                else
                {
                    int.TryParse(strs2[1], out n);
                }
                string str4 = strs2[0].Substring(0, strs2[0].IndexOf('-'));
                if (strs2.Length > 2)
                    style = strs2[2];
                else
                    style = "智能传感器";  
                for (int i = 0; i < n; i++)
                {
                    string str3 = str4 + "-" + n0++;
                    dic.Add(str3,style);
                }
            }
            return dic;
        }
        private List<string> GetJkbNos(string strSec)
        {
            int I_n, Jk_n, I_start=0, Jk_start;
            string Istr,strJk_Gl="";
            List<string> list = new List<string>();
            string str = m_myini.GetString(strSec, "接口板编号", "");
            string[] strs1 = str.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string str2 in strs1)
            {
                string[] strs2 = str2.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                int.TryParse(strs2[1], out Jk_n);
                int t1,t2;
                t1=strs2[0].IndexOf('-');
                t2 = strs2[0].LastIndexOf('-');
                if (strs2[0].Contains("JK"))
                {
                    int.TryParse(strs2[0].Substring(t1 + 1, strs2[0].IndexOf("JK") - (t1+1)), out I_start);
                    strJk_Gl = "JK";
                }
                if (strs2[0].Contains("GL"))
                {
                    int.TryParse(strs2[0].Substring(t1 + 1, strs2[0].IndexOf("GL") - (t1+1)), out I_start);
                    strJk_Gl = "GL";
                }
                if (strs2[2].Contains("-"))
                {
                    string str5 = strs2[1].Substring(strs2[1].IndexOf('-'), strs2[1].Length - strs2[1].IndexOf('-') - 1);
                    int.TryParse(str5,out I_n);
                    I_n = I_n - I_start + 1;
                }
                else
                {
                    int.TryParse(strs2[2], out I_n);
                }
               
                int.TryParse(strs2[0].Substring(t2+1), out Jk_start);
                Istr = strs2[0].Substring(0, t1);
                for (int i = 0; i < I_n; i++)
                {
                    for(int j=0;j<Jk_n;j++)
                    {
                        string str3 = Istr.Trim() + "-" + (I_start + i) + strJk_Gl + "-" + (Jk_start + j);
                        list.Add(str3);
                    }
                }
            }
            return list;
        }
        /// <summary>
        /// 读取ini文件，对电源环线进行配置
        /// </summary>
        private void SetCfg()
        {
            cgqHangjianJu = m_myini.GetInt16("配置", "智能传感器列间距", 0);
            jkbHangJIanJu = m_myini.GetInt16("配置", "接口板列间距", 0);
            cgq_jkbHangjianju = m_myini.GetInt16("配置", "智能传感器接口板行间距", 0);
            zJIanju = m_myini.GetInt16("配置", "组于组行间距", 0);
             int n1,n2;
            m_dymodle_count = m_myini.GetInt16("电源模块", "sum", 0);
            for (int i = 0; i < m_dymodle_count; i++)
            {
                n1 = m_myini.GetInt16("电源模块" + (i+1), "智能传感器", 0);
                n2 = m_myini.GetInt16("电源模块" + (i+1), "接口板", 0);
                List<MyTableJdqMini> jdqlist = new List<MyTableJdqMini>();
                List<MyTableJkbMini> jkblist = new List<MyTableJkbMini>();
                MyTableDy dy = new MyTableDy(50, tuzhi.Top-i * zJIanju, tuzhi);
                if (dy.m_TableNameDic.ContainsKey("name"))
                {
                    dy.m_TableNameDic["name"].Text = "电源模块" + (i + 1);
                }
                dyList.Add(dy);
               // List<string> strlist = GetCgqNos("电源模块" + (i + 1));
                Dictionary<string, string> dic = GetCfgNo_Style("电源模块" + (i + 1));
                int j = 0;
                //for (int j = 0; j < dic.Count; j++)
                foreach(KeyValuePair<string,string> p in dic)
                {
                    MyTableJdqMini jd=new MyTableJdqMini(j * cgqHangjianJu + 100, tuzhi.Top - i * zJIanju, tuzhi);
                    jd.m_TableNameDic["no"].Text = "板号：" + p.Key;
                    jd.m_TableNameDic["name"].Text = p.Value;
                    jd.m_TableNameDic["no"].Height = 1.5;
                    jd.m_TableNameDic["name"].Height=1.5;
                    jdqlist.Add(jd);
                    j++;
                }
                j = 0;
                List<string> strlist = GetJkbNos("电源模块" + (i + 1));
                for (int k = 0; k < strlist.Count; k++)
                {
                    MyTableJkbMini jkb = new MyTableJkbMini(k * jkbHangJIanJu + 100, tuzhi.Top - (i * zJIanju + cgq_jkbHangjianju), tuzhi);
                    jkblist.Add(jkb);
                    jkb.m_TableNameDic["no"].Text = strlist[k];
                    jkb.m_TableNameDic["no"].Height = 1.2;
                }
                jkbDic.Add(i,jkblist);
                cgqDic.Add(i, jdqlist);
            }
        }
        public override void MyDraw(MyCadDll.MyCad mycad)
        {
            base.MyDraw(mycad);
            foreach (MyTableDy dy in dyList)
            {
                dy.MyDraw(mycad);
            }
            for (int i = 0; i < m_dymodle_count; i++)
            {
                foreach (MyTableJdqMini jdq in cgqDic[i])
                {
                    jdq.MyDraw(mycad);
                }
            }
            for (int j = 0; j < m_dymodle_count; j++)
            {
                foreach (MyTableJkbMini jkb in jkbDic[j])
                {
                    jkb.MyDraw(mycad);
                }
            }
            foreach (MyLine line in m_lianXian)
            {
                line.MyDraw(mycad);
            }
            
        }

    }
}
